package com.financing.wap.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.financing.wap.weixin.dto.SendRedPack;
import com.financing.wap.weixin.util.WeixinSign;


/**
 * 客户风险评估
 * @author lisu
 *
 */
public class WeChatGiftXMLUtil {
	static Logger logger = Logger.getLogger(WeChatGiftXMLUtil.class);
	
	
	public final static String ELEMENT_MCH_ID= "mch_id";
	public final static String ELEMENT_NICK_NAME = "nick_name";
	public final static String ELEMENT_SEND_NAME = "send_name";
	public final static String ELEMENT_WISHING = "wishing";
	public final static String ELEMENT_ACT_NAME = "act_name";
	public final static String ELEMENT_REMARK = "remark";
	public final static String ELEMENT_LOGO_IMGURL = "logo_imgurl";
	public final static String ELEMENT_SHARE_CONTENT = "share_content";
	public final static String ELEMENT_SHARE_URL = "share_url";
	public final static String ELEMENT_SHARE_IMGURL = "share_imgurl";
	public final static String ELEMENT_CLIENT_IP = "client_ip";
	public final static String ELEMENT_TOTAL_NUM = "total_num";
	public final static String RED_PACK_KEY = "key";
	public final static String CERT_PATH = "cert_path";
	public final static String MP_ANTH_BACK = "mpAnthBack";
	
	
	
	//微信红包公共基本信息
	public static Map<String,String> map = new HashMap<String,String>();
	//微信红包模板基本信息
	public static Map<String,Map<String,String>> templteMap = new HashMap<String,Map<String,String>>();
	
	static{
		Logger logger = Logger.getLogger(MessageUtil.class);
		SAXReader  saxReader = new SAXReader();
		try {
			Document document = saxReader.read(WeChatGiftXMLUtil.class.getResourceAsStream("/WeChatGift.xml"));
			Element rootElement=document.getRootElement();  

			
			for(@SuppressWarnings("unchecked") Iterator<Element> typeElementIterator = rootElement.elementIterator();typeElementIterator.hasNext();){

				Element valueElement = typeElementIterator.next();
				
				String id = valueElement.attributeValue("code");
				Map<String,String> messageMap = new HashMap<String,String>();
				
				for(@SuppressWarnings("unchecked") Iterator<Element> valueElementIterator = valueElement.elementIterator();valueElementIterator.hasNext();){
					Element valuesElement = valueElementIterator.next();
					if(ELEMENT_MCH_ID.equals(valuesElement.getName())){
						map.put(ELEMENT_MCH_ID, valuesElement.getData().toString());
					}
					if(CERT_PATH.equals(valuesElement.getName())){
						map.put(CERT_PATH, valuesElement.getData().toString());
					}
					if(RED_PACK_KEY.equals(valuesElement.getName())){
						map.put(RED_PACK_KEY, valuesElement.getData().toString());
					}
					messageMap.put(valuesElement.getName(), valuesElement.getData().toString());
					
				}
				templteMap.put(id, messageMap);
			}
			
		} catch (DocumentException e) {
			logger.error("Init allQuestionsList error :", e);
			throw new RuntimeException(e);
		}
		
	}
	public static SendRedPack getSendRedPack(){
		return getSendRedPack("PT");
	}
	public static SendRedPack getSendRedPack(String key){
		Map<String,String> templteData =  templteMap.get(key);
		SendRedPack bean =  new SendRedPack();
		bean.setMch_id(templteData.get(ELEMENT_MCH_ID));
		bean.setSend_name(templteData.get(ELEMENT_SEND_NAME));
		bean.setWishing(templteData.get(ELEMENT_WISHING));
		bean.setAct_name(templteData.get(ELEMENT_ACT_NAME));
		bean.setRemark(templteData.get(ELEMENT_REMARK));
		bean.setClient_ip(templteData.get(ELEMENT_CLIENT_IP));
		bean.setTotal_num(Integer.valueOf(templteData.get(ELEMENT_TOTAL_NUM)));
		bean.setWxappid(PropertiesUtils.getValue("mp_appid"));
		bean.setMch_billno(bean.getMch_id()+DateUtil.getDate()+RandomUtils.getRandomNumUtilsForString(10));
		bean.setNonce_str(WeixinSign.create_nonce_str());
		return bean;
	}
	
}

